001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.library.info;
020    
021    import java.util.Arrays;
022    import java.util.Properties;
023    
024    /**
025     * The ModuleDirective class describes a module data-structure.
026     *
027     * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a>
028     * @version 1.0.0
029     */
030    public final class ModuleDirective extends ResourceDirective
031    {
032        private final ResourceDirective[] m_resources;
033        
034       /**
035        * Creation of a new module directive.  If the resource name if composite
036        * then the resource directive will be a module directive instance that either 
037        * encloses the resource or enclosed a resource containing the resource.
038        *
039        * @param name the resource name
040        * @param version the resource version
041        * @param classifier LOCAL or EXTERNAL classifier
042        * @param basedir the project basedir
043        * @param info info descriptor
044        * @param data datatypes produced by the resource
045        * @param dependencies resource dependencies
046        * @param properties suppliementary properties
047        * @param filters project filters
048        * @param resources subsidary resources
049        * @return the immediate enclosing resource
050        */
051        public static ModuleDirective createModuleDirective( 
052          String name, String version, Classifier classifier, String basedir, 
053          InfoDirective info, DataDirective[] data, 
054          DependencyDirective[] dependencies, Properties properties, 
055          FilterDirective[] filters, ResourceDirective[] resources )
056        {
057            int n = name.indexOf( "/" );
058            if( n > -1 )
059            {
060                ModuleDirective enclosing = null;
061                String[] elements = name.split( "/", -1 );
062                for( int i = ( elements.length-1 ); i>-1; i-- )
063                {
064                    String elem = elements[i];
065                    if( i == ( elements.length-1 ) )
066                    {
067                        enclosing =  
068                          new ModuleDirective(
069                            elem, version, classifier, basedir, info, data, dependencies,
070                            resources, properties, filters );
071                    }
072                    else
073                    {
074                        enclosing = 
075                          new ModuleDirective(
076                            elem, null, Classifier.EXTERNAL, ".", null,
077                            new DataDirective[0], new DependencyDirective[0],
078                            new ResourceDirective[]{enclosing}, null, null );
079                    }
080                }
081                return enclosing;
082            }
083            else
084            {
085                return new ModuleDirective(
086                  name, version, classifier, basedir, info, data, dependencies,
087                  resources, properties, filters );
088            }
089        }
090        
091       /**
092        * Creation of a new module directive supporting the establishment
093        * of an anonymous resource.
094        *
095        * @param name the module name
096        * @param resource resource contained within the module
097        */
098        public ModuleDirective( String name, ResourceDirective resource )
099        {
100            this(
101              name, null, Classifier.ANONYMOUS, null, null,
102              new DataDirective[0], new DependencyDirective[0],
103              new ResourceDirective[]{resource}, null, null );
104        }
105        
106       /**
107        * Creation of a new module directive.
108        * @param name the resource name
109        * @param version the resource version
110        * @param classifier LOCAL or EXTERNAL classifier
111        * @param basedir the project basedir
112        * @param info info descriptor
113        * @param data datatypes produced by the resource
114        * @param dependencies resource dependencies
115        * @param resources resource included within the module
116        * @param properties suppliementary properties
117        * @param filters project filters
118        */
119        public ModuleDirective(
120          String name, String version, Classifier classifier, String basedir, 
121          InfoDirective info, DataDirective[] data,
122          DependencyDirective[] dependencies, ResourceDirective[] resources,
123          Properties properties, FilterDirective[] filters )
124        {
125            super( name, version, classifier, basedir, info, data, dependencies, properties, filters );
126            
127            if( null == resources )
128            {
129                throw new NullPointerException( "resources" );
130            }
131            for( int i=0; i<resources.length; i++ )
132            {
133                if( null == resources[i] )
134                {
135                    throw new NullPointerException( "resource [" + i + "]" );
136                }
137            }
138            m_resources = resources;
139        }
140        
141       /**
142        * Return an array of resource directives representing the resources within 
143        * the module.
144        * @return the nested resource directives
145        */
146        public ResourceDirective[] getResourceDirectives()
147        {
148            return m_resources;
149        }
150        
151       /**
152        * Compare this object with another for equality.
153        * @param other the other object
154        * @return true if equal
155        */
156        public boolean equals( Object other )
157        {
158            if( super.equals( other ) && ( other instanceof ModuleDirective ) )
159            {
160                ModuleDirective object = (ModuleDirective) other;
161                return Arrays.equals( m_resources, object.m_resources );
162            }
163            else
164            {
165                return false;
166            }
167        }
168        
169       /**
170        * Compute the hash value.
171        * @return the hashcode value
172        */
173        public int hashCode()
174        {
175            int hash = super.hashCode();
176            hash ^= super.hashArray( m_resources );
177            return hash;
178        }
179    }